Hi all, this afternoon i wrote a code and run in the Geany program though i do not have the full code. I just want work out why the result i calculated was not correct. The code written in the program is:
Code:
#include <stdio.h>
unsigned int n;
unsigned int m, m1, m2, m3, m4, m5, m6;
unsigned int z1, z2, z3, z4, z5, z6;
unsigned int MB32(unsigned int x)
{
n = (unsigned char) x; // get last char of x, n == 'cd'.
m = (unsigned char) x;
n <<= 8; // n == 'cd00'
m1 = n; //
z1 = x; // z1 == x == '12 34 ab cd'
n |= (unsigned char) (x >> 8);
m2 = n; //display the 1st result of n;
z2 = x; //display the 1st result of x;
//=================================================
n <<= 8;
m3 = n;
z3 = x;
n |= (unsigned char) (x >> 16);
m4 = n;
z4 = x;
//=================================================
//return (n << 8) | (unsigned char) (x >> 24);
n <<= 8;
m5 = n;
z5 = x;
n |= (unsigned char) (x >> 24);
m6 = n;
z6 = x;
return n;
}
int main()
{
//int BlockIn = 0xABCD1234;
int BlockIn = 0x1234ABCD;
int BlockOut = MB32(BlockIn);
printf ("BlockIn is: %x \n", BlockIn);
printf ("BlockOut is: %x \n", BlockOut);
printf ("m = %x \n", m);
printf ("m1 = %x \n", m1);
printf ("z1 = %x \n \n", z1);
printf ("m2 = %x \n", m2);
printf ("z2 = %x \n \n", z2);
printf ("m3 = %x \n", m3);
printf ("z3 = %x \n \n", z3);
printf ("m4 = %x \n", m4);
printf ("z4 = %x \n \n", z4);
printf ("m5 = %x \n", m5);
printf ("z5 = %x \n \n", z5);
printf ("m6 = %x \n", m6);
printf ("z6 = %x \n \n", z6);
return 0;
}
I think only the reason i got the wrong answer is because of the definition of 'unsigned char x', 'unsigned int x', and 'unsigned int n'. Some comment were added. I wish this can be used for other learner as well. This could could be run under 64bit Ubuntu.
Thank you all guys!